package com.yonyou.crm.bpub.detail.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.crm.bpub.detail.entity.*;
import com.yonyou.crm.common.biz.service.BaseBizServiceImpl;
import com.yonyou.crm.common.exception.CrmBusinessException;
import com.yonyou.crm.common.exception.CrmMessageException;

@Service
public class DetailsBizServiceImpl extends BaseBizServiceImpl<DetailsVO>{
	
	@Autowired
	CustomerDetails customerDetailService;
	
	@Autowired
	ContactDetails contactDetailService;
	
	@Autowired
	LeadDetails leadDetailService;
	
	@Autowired
	OppDetail oppDetail;
	
	@Autowired
	CumprmtDetails cumprmtDetail;
	
	public <T extends Serializable> DetailsVO getDetailData(String module, String id) throws CrmMessageException{
		DetailsVO  detailData= new DetailsVO();
		BaseDetails service = null;
		List<DetailsGroupVO> header = new ArrayList<>();
		List<DetailsGroupVO> body = new ArrayList<>();
		List<DetailsFlagVO> flag = new ArrayList<>();
		List<String> action = new ArrayList<>();
		long idValue = 0;
		T obj = null;
		Map<String, List<DetailsGroupVO>> layoutList = null;
		switch (module) {
			case "accounts" :
				service = customerDetailService;
				layoutList = service.getLayout(obj);
				break;
			case "contacts" :
				service = contactDetailService;
				break;
			case "opportunities" :
				service = oppDetail;
				break;
			case "leads" :
				service = leadDetailService;
				break;
			case "cumprmts":
				service = cumprmtDetail;
				break;
			default:
				//nothing
		}
		idValue = Long.valueOf(id);
		obj = service.getDetail(idValue);
		if (obj == null) {
			throw new CrmBusinessException("对象不存在");
		}
		header = service.getHeader(obj, layoutList);
		body = service.getBody(obj, layoutList);
		flag = service.getFlag(obj);
		action = service.getAction(obj);
		
		detailData.setHeaderlist(header);
		detailData.setBodylist(body);
		detailData.setFlags(flag);
		detailData.setActions(action);
		
		return detailData;
	}

}
